package vn.personal.anhht.bloback12.activity;

import vn.personal.anhht.bloback12.util.Constants;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;

public class TabSettingCallActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{

    private Resources rs;
    private CheckBoxPreference checkPref;
    private ListPreference listPref;
    private ListPreference listPref1;
    private ListPreference listPref2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(Constants.SETTINGS_SHARED_PREFERENCES_NAME);
        rs = getResources();
        addPreferencesFromResource(R.xml.preferences_call);
        initSettings();
        
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
    
    private void initSettings() {
        checkPref = (CheckBoxPreference) findPreference(rs.getString(R.string.call_pref_turn_key));
        
        listPref = (ListPreference) findPreference(rs.getString(R.string.call_pref_mode_block_key));
        
        listPref1 = (ListPreference) findPreference(rs.getString(R.string.call_pref_mode_inform_key));
        
        listPref2 = (ListPreference) findPreference(rs.getString(R.string.call_pref_phone_state_key));
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        
        Preference pref = findPreference(key);
        if (pref instanceof CheckBoxPreference) {
            CheckBoxPreference cbp = (CheckBoxPreference) pref;
            if((rs.getString(R.string.call_pref_turn_key)).equals(key)){
                if (cbp.isChecked()) {
                    pref.setSummary(rs.getString(R.string.call_pref_turn_summary_1));
                } else {
                    pref.setSummary(rs.getString(R.string.call_pref_turn_summary));
                }
            }
            
        }
        
        if(pref instanceof ListPreference){
            ListPreference lp = (ListPreference) pref;
            if((rs.getString(R.string.call_pref_mode_block_key)).equals(key)){
                String[] arrayModeLockValue = rs.getStringArray(R.array.entries_values_list_lockmode_call);
                String[] arrayModeLock = rs.getStringArray(R.array.entries_list_lockmode_call);
                if(lp.getValue().equals(arrayModeLockValue[0])){
                    lp.setSummary(arrayModeLock[0]);
                }else if(lp.getValue().equals(arrayModeLockValue[1])){
                    lp.setSummary(arrayModeLock[1]);
                }else{
                    lp.setSummary(arrayModeLock[2]);
                }

            }
            
            if((rs.getString(R.string.call_pref_mode_inform_key)).equals(key)){
                String[] arrayModeInformValue = rs.getStringArray(R.array.entries_values_list_inform_call);
                String[] arrayModeInform = rs.getStringArray(R.array.entries_list_inform_call);
                if(lp.getValue().equals(arrayModeInformValue[0])){
                    lp.setSummary(arrayModeInform[0]);
                }else{
                    lp.setSummary(arrayModeInform[1]);
                }
            }
            
            if((rs.getString(R.string.call_pref_phone_state_key)).equals(key)){
                String[] arrayPhoneStateValue = rs.getStringArray(R.array.entries_values_list_state_call);
                String[] arrayPhoneStateInform = rs.getStringArray(R.array.entries_list_state_call);
                if(lp.getValue().equals(arrayPhoneStateValue[0])){
                    lp.setSummary(arrayPhoneStateInform[0]);
                }else{
                    lp.setSummary(arrayPhoneStateInform[1]);
                }
            }
        }
        
    }

    @Override
    protected void onStart() {
        Log.e("TabSettingCallActivity", "onStart");
        super.onStart();
        if(checkPref.isChecked()){
            checkPref.setSummary(rs.getString(R.string.call_pref_turn_summary_1));
        }
        if(!checkPref.isChecked()){
            checkPref.setSummary(rs.getString(R.string.call_pref_turn_summary));
        }
        String[] arrayModeLockValue = rs.getStringArray(R.array.entries_values_list_lockmode_call);
        String[] arrayModeLock = rs.getStringArray(R.array.entries_list_lockmode_call);
        if(listPref.getValue() != null){
            if(listPref.getValue().equals(arrayModeLockValue[0])){
                listPref.setSummary(arrayModeLock[0]);
            }else if(listPref.getValue().equals(arrayModeLockValue[1])){
                listPref.setSummary(arrayModeLock[1]);
            }else{
                listPref.setSummary(arrayModeLock[2]);
            }
        }
        String[] arrayModeInformValue = rs.getStringArray(R.array.entries_values_list_inform_call);
        String[] arrayModeInform = rs.getStringArray(R.array.entries_list_inform_call);
        if(listPref1.getValue().equals(arrayModeInformValue[0])){
            listPref1.setSummary(arrayModeInform[0]);
        }else{
            listPref1.setSummary(arrayModeInform[1]);
        }
        String[] arrayPhoneStateValue = rs.getStringArray(R.array.entries_values_list_state_call);
        String[] arrayPhoneStateInform = rs.getStringArray(R.array.entries_list_state_call);
        if(listPref2.getValue().equals(arrayPhoneStateValue[0])){
            listPref2.setSummary(arrayPhoneStateInform[0]);
        }else{
            listPref2.setSummary(arrayPhoneStateInform[1]);
        }
    }
}

